<?php
Database($_SESSION['cid']);
class AdminAction extends Action{
		
	//列表页面
	public function index(){
		$righs = D('Admin')->get_rights($_SESSION['id']);
		if($righs!=1)
			$this->error('当前权限不够!');
		
		$rs=pageing('admin',7,'',$_GET['seach'],'admin_id desc','admin_cid='.$_SESSION['cid'],'admin_name');
		foreach($rs['data'] as $val){
			$val['time_insert']=date("Y-m-d",$val['time_insert']);
			$arr[]=$val;
		}
		$rs['data']=$arr;
		$this->assign("rs",$rs);
		$this->display();
	}

    //添加页面
    public function to_add(){
		$righs = D('Admin')->get_rights($_SESSION['id']);
		if($righs!=1)
			$this->error('当前权限不够!');
		$rs['meeting_category'] = D('Meeting')->get_meeting_categorys();
		$this->assign('rs',$rs);
        $this->display();
    }
	
	public function addAct(){
		$rs = D('Admin')->add_admin($_POST);
		if($rs){
			$this->success('添加成功',__APP__.'/Company/Admin/index');
		}else{
			$this->error('添加失败!');	
		}
	}

	//删
	public function del_news(){
		$result = M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->find();
        $result = M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->delete();
		if ($result){
			echo '{"state":1}';
		}else{
			echo '{"state":0}';
		}
    }
	
	//改
	public function to_update(){
		$news = M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->find();
		$this->assign('news',$news);
		$rs['channel']=D('Base/Channel')->get_all();
		$this->assign("rs",$rs);
		$this->display();
	}

	public function update_news(){
		if($_FILES['news_image']['name']!=''){
			$this->imgUpload();
		}else{
			$rs = M('news','','DB_MEETING')->where('news_id='.$_POST['news_id'])->find();
			$_POST['news_image']	= $rs['news_image'];
			$_POST['news_image_s']	= $rs['news_image_s'];
		}
		$rs = M('news','','DB_MEETING')->where('news_id='.$_POST['news_id'])->find();
		$text=strip_tags($_POST['news_content']);
		$text = str_replace("&","",$text);
		$text = str_replace("<","",$text);
		$text = str_replace(">","",$text);
		$text = str_replace("\n","",$text);
		$text = str_replace("\r","",$text);
		$text = str_replace("\t","",$text);
		$_POST['news_intro'] = mb_substr($text,0,30,'UTF-8');
		$html=file_get_contents($this->tpl_path);
		$html=str_replace('<!--news_title-->',$_POST['news_title'],$html);
		if($_POST['news_image']!=''){
			$html=str_replace('<!--news_image-->',"<img width='100%' src='".C('DOMAIN_NAME').__ROOT__.'/'.$_POST['news_image']."' />",$html);
		}
		$html=str_replace('<!--news_time-->',date('Y-m-d',$rs['news_time']),$html);
		$_POST['news_author']=$_POST['news_author']==''?'匿名':$_POST['news_author'];
		$html=str_replace('<!--news_author-->',$_POST['news_author'],$html);
		$html=str_replace('<!--news_content-->',$_POST['news_content'],$html);
		$html=str_replace('<!--news_pid-->',$_SESSION['cid'],$html);
		$news_image_path = 'Public/Uploads'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= 'news'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= $_SESSION['cid'].'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_path.= $news_image_path.time().rand(0,10000).'.html';
		file_put_contents($news_path,$html);
		
		$_POST['news_url'] = $news_path;
		$rs = M('news','','DB_MEETING')->where('news_id='.$_POST['news_id'])->save($_POST);
		if($rs){
			$this->success("Update Success!");
		} else {
			$this->error("update Fail for database");
		}
	}
	
	public function imgUpload(){
		
		$news_image_path = 'Public/Uploads'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= 'news'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= $_SESSION['cid'].'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);

		import('ORG.Net.UploadFile');
		$config['savePath'] = $news_image_path;
		$config['allowExts']= array('jpg', 'gif', 'png', 'jpeg');
		$config['maxSize']= 3145728;
		$config['thumb'] = true;
		$config['thumbPrefix'] = 'm_,s_';
		$config['thumbMaxWidth'] = '480,240';
		$config['thumbMaxHeight'] = '320,160';
		$config['thumbRemoveOrigin']=true;
		$upload = new UploadFile($config);
		if(!$upload->upload()){
			$this->error($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['news_image']	=$news_image_path.'m_'.$uploadList[0]['savename'];
			$_POST['news_image_s']	=$news_image_path.'s_'.$uploadList[0]['savename'];
		}
	}

	public function news_display(){
		$rs=M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->save(array('news_display'=>$_GET['news_display']));
		if($rs)
			echo '{"state":0}';
		else
			echo '{"state":-1}';
	}

	public function check_suggest(){
		$rs=pageing('news_comment',1000,'',$_GET['seach'],$_GET['order'],'news_id='.$_GET['news_id'],$col='content',$now='check_suggest');
		foreach($rs['data'] as $val){
			$val['time_insert']=date("Y-m-d H:i",$val['time_insert']);
			$arr[]= $val;
		}
		$rs['data']=$arr;
		$this->assign("rs",$rs);
		$this->display();
	}
}
?>